package com.woniuxy.demo.configuration;

import com.woniuxy.demo.constant.MyImageConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 佘赛飞
 * @create 2021 -10 -10 10:26 AM
 */
@Configuration
public class MyWebMvcConfiguration {

//    @Value("${myimage.resourceHandlers}")
//    private String  resourceHandler;
//
//    @Value("${myimage.resourceLocations}")
//    private String  resourceLocations;
//
//    @Bean
//    public WebMvcConfigurer mvcConfigurer(){
//        return new WebMvcConfigurer() {
//            @Override
//            public void addResourceHandlers(ResourceHandlerRegistry registry) {
//                //将请求路径映射到真实的物理路径
//                registry.addResourceHandler(resourceHandler).
//                        addResourceLocations(resourceLocations);
//            }
//        };
//    }

//方法二使用静态方法
    @Bean
    public WebMvcConfigurer mvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                //将请求路径映射到真实的物理路径
                registry.addResourceHandler(MyImageConstant.RESOURCEHANDLER)
                        .addResourceLocations(MyImageConstant.RESOURCELOCATIONS);
            }
        };
    }
}
